//------------------------------------------------------
//--                                                	--
//--				www.riemers.net			--
//--				Basic shaders			--
//--			Use/modify as you like              --
//--                                                	--
//------------------------------------------------------

struct VertexToPixel
{
    float4 Position   	: POSITION;    
    float4 Color		: COLOR0;
    
    float3 Normal:		TEXCOORD0;
    float3 ViewVec:		TEXCOORD1;
    float3 WorldPos:	TEXCOORD2;
};

struct PixelToFrame
{
    float4 Color : COLOR0;
};
struct Light
{
	float3 Position;
	float4 Color;
};
//------- XNA-to-HLSL variables --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;
float xDiffuseK;
float xSpecularK;
float xShine;
bool xEnableLighting;
bool xShowNormals;
float3 xCameraPosition;
float3 xViewVector = float3(1, 0, 0);
Light xLightArray;

//------- Texture Samplers --------

Texture xTexture;
sampler TextureSampler = sampler_state { texture = <xTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};


//------- Technique: Colored --------

VertexToPixel ColoredVS( float4 inPos : POSITION, float4 inColor: COLOR, float3 inNormal: NORMAL)
{	
	VertexToPixel Output = (VertexToPixel)0;
	float4x4 preViewProjection = mul (xView, xProjection);
	float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
	Output.Position = mul(inPos, preWorldViewProjection);
	Output.Color = inColor;
	float3 Light = xLightArray.Position - mul(inPos, xWorld);
	Output.WorldPos = mul(inPos, xWorld);
	float3 Normal = normalize(mul(normalize(inNormal), xWorld));
	Output.Normal = Normal;
	Output.ViewVec = normalize(xCameraPosition - mul(inPos, xWorld));
	if (xEnableLighting)
	{
		float NdotL = dot(Normal, Light);
		float diffuse = xDiffuseK * NdotL;
		//Output.Color  = saturate(inColor * diffuse);
	}
	return Output;    
}

PixelToFrame ColoredPS(VertexToPixel input) 
{
	PixelToFrame Output = (PixelToFrame)0;
	float3 Light = normalize(xLightArray.Position - input.WorldPos);
	float3 Normal = normalize(input.Normal);
	float3 V = input.ViewVec;
	float3 H = normalize(Light + V);
	float NdotL = dot(Normal, Light);
	float NdotH = dot(H, Normal);
	float4 specular = xSpecularK * xLightArray.Color * max(pow(NdotH, xShine), 0);
	float4 diffuse = xDiffuseK * NdotL * input.Color * xLightArray.Color;
	Output.Color = saturate(diffuse + specular + xAmbient);
	return Output;
}

technique Colored_2_0
{
	pass Pass0
	{   
		VertexShader = compile vs_2_0 ColoredVS();
		PixelShader  = compile ps_2_0 ColoredPS();
	}
}

technique Colored
{
	pass Pass0
	{
		VertexShader = compile vs_1_1 ColoredVS();
		PixelShader  = compile ps_2_0 ColoredPS();
	}
}

//---------------TECHNIQUE TEST1-----------------------------

VertexToPixel Test1VS( float4 inPos : POSITION, float4 inColor: COLOR, float3 inNormal: NORMAL)
{	
	VertexToPixel Output = (VertexToPixel)0;
	float4x4 preViewProjection = mul (xView, xProjection);
	float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
	Output.Position = mul(inPos, preWorldViewProjection);
	Output.Color = inColor;
	float3 Light = xLightArray.Position - mul(inPos, xWorld);
	Output.WorldPos = mul(inPos, xWorld);
	float3 Normal = normalize(mul(normalize(inNormal), xWorld));
	Output.Normal = Normal;
	Output.ViewVec = normalize(xCameraPosition - mul(inPos, xWorld));
	if (xEnableLighting)
	{
		float NdotL = dot(Normal, Light);
		float diffuse = xDiffuseK * NdotL;
		//Output.Color  = saturate(inColor * diffuse);
	}
	return Output;    
}

PixelToFrame Test1PS(VertexToPixel input, float2 uv: TEXCOORD) 
{
	PixelToFrame Output = (PixelToFrame)0;
	float3 Light = normalize(xLightArray.Position - input.WorldPos);
	float3 Normal = normalize(input.Normal);
	float3 V = input.ViewVec;
	float3 H = normalize(Light + V);
	float NdotL = dot(Normal, Light);
	float NdotH = dot(H, Normal);
	float4 specular = xSpecularK * xLightArray.Color * max(pow(NdotH, xShine), 0);
	float4 diffuse = xDiffuseK * NdotL * input.Color * xLightArray.Color;
	Output.Color = saturate(diffuse + specular + xAmbient);
	Output.Color.r = Output.Color.r*sin(uv.x*100)*2; 
	Output.Color.g = Output.Color.g*cos(uv.x*150)*2; 
	Output.Color.b = Output.Color.b*sin(uv.x*50)*2;
	return Output;
}
technique Test1
{
	pass Pass0
	{
		VertexShader = null;
		PixelShader  = compile ps_2_0 Test1PS();
	}
}
